<template>
  <Layout>
      <Row :gutter="16" class="query-form" style="margin-left: 10px">
          <span style="font-size: 16px">重大事故隐患排查</span>
          <Button class="ivu-btn-reset" @click="goback" icon="md-arrow-back">返回</Button>
      </Row>
      <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!" :span-method="handleSpan" :loading='loading'
             border :columns="columns1" :data="dataTable">
          <template slot-scope="{ row, index }" slot="ifProblem">
              <!-- <div v-if="view || !row.contentId">{{row.ifProblem}}</div> -->
              <!-- <Input v-model="row.ifProblem" style="width: 100%"></Input> -->
              <i-switch v-model="row.ifProblem" true-value="0" false-value="1" @on-change="tableChange(row,index)">
                <span slot="open">是</span>
                <span slot="close">否</span>
            </i-switch>
          </template>
          <template slot-scope="{ row, index }" slot="handleDesc">
              <!-- <div v-if="view || !row.contentId">{{row.handleDesc}}</div> -->
              <Input v-model="row.handleDesc" type="textarea" :autosize="{minRows:2,maxRows:5}" style="width: 100%"></Input>
          </template>
          <template slot-scope="{ row, index }" slot="action">
            <!-- <div v-if="view || !row.contentId"></div> -->
            <Button size="small" type="primary" @click="saveEvaluate(row,index)">保存</Button>
          </template>
      </Table>
  </Layout>
</template>

<script>
  import {Api} from '@/api/index.js'
  import {formatDate} from '@/tools/index'
  import pageCommonFunction from '@/components/pageCommonFunction.js'
  import {sessionStorage, cookie} from '@/common/cacheUtils.js';

  export default {
      components: {},
      data() {
          return {
              ...this.createCommonDataField(),
              columns1: [
                  // {title: '序号', width: 70, fixed: 'left', align: 'center', render: this.indexColumnsRender},
                  { key:'standardOrder',title: '标准名称序号',  align: 'center', width: 90 },
                  { key:'standardName',title: '标准名称',  align: 'center', minWidth: 100,tree: true},
                  { key:'twoDirectory',title: '二级目录',  align: 'center', minWidth: 80  },
                  { key:'orderNum',title: '分序',  align: 'center', width: 80},
                  { key:'screenStandard',title: '排查标准',  align: 'center', minWidth: 120,tooltip:true  },
                  { slot:'ifProblem',title: '是否存在问题',  align: 'center', width: 100,  },
                  { slot:'handleDesc',title: '处理情况',  align: 'center', minWidth: 180,  },
                  // {title: '更新时间', width: 100, align: 'center', key:'updateDate',render:this.renderCellDate},
                  // {title: '填报人', width: 100, align: 'center', key:'updateUserName'},
                  {title: '操作', width: 65, align: 'center',  slot: 'action'},
              ],
              tableHeight: 10,
              // api
              getUrl: Api.getAccHazardScreenCompany,
            //   batchUpdateUrl: Api.batchUpdateAccHazardScreenCompany,
              itemUpdateUrl: Api.itemUpdateAccHazardScreenCompany,

            //   _dataTable:[],
              loading: false
          }
      },
      watch:{},
      created() {
          this.tableHeight = window.innerHeight -135 - 50
          this.query();
      },
      methods: {
          ...pageCommonFunction,
          goback(){
              this.$emit("close",true);
          },
                  
          handleSpan ({ row, column, rowIndex, columnIndex }) {
              if (columnIndex === 0 || columnIndex === 1) {
                  let x = row.mergeColumn === 0 ? 0:row.mergeColumn
                  let y = row.mergeColumn === 0 ? 0:1
                  return [x, y]
              } else if (columnIndex === 2) {
                  let x = row.mergeColumn1 === 0 ? 0:row.mergeColumn1
                  let y = row.mergeColumn1 === 0 ? 0:1
                  return [x, y]
              } 
          },
          getTableDate(dataTable){
            let standardOrders = []
            let twoDirectorys = []
            dataTable.forEach(e => {
              e._twoDirectory = e.standardOrder + '-' + e.twoDirectory
                if(!standardOrders.includes(e.standardOrder)){
                    standardOrders.push(e.standardOrder)
                }
                if(!twoDirectorys.includes(e._twoDirectory)){
                    twoDirectorys.push(e._twoDirectory)
                }
            })
            let nameNums = []
            //将names数组中的 name值设置默认合并0个单元格,放到 nameNums中
            standardOrders.forEach(e => {
                nameNums.push({standardOrder:e,num:0})
            })
            
            let monthNums = []
            twoDirectorys.forEach(e => {
                monthNums.push({_twoDirectory:e,num:0})
            })
            //计算每种 name值所在行需要合并的单元格数
            dataTable.forEach(e => {
                nameNums.forEach(n => {
                    if(e.standardOrder === n.standardOrder){
                        n.num++
                    }
                })
                monthNums.forEach(n => {
                    if(e._twoDirectory === n._twoDirectory){
                        n.num++
                    }
                })
            })
            dataTable.forEach(e => {
                // if (!e.completionProgress) {
                //     e.updateDate = null
                //     e.updateUserName = null
                // }
                nameNums.forEach(n => {
                    if(e.standardOrder === n.standardOrder){
                        if(standardOrders.includes(e.standardOrder)){
                            e.mergeColumn = n.num
                            //删除已经设置过的值(防止被合并的单元格进到这个 if 语句中)
                            standardOrders.splice(standardOrders.indexOf(n.standardOrder),1)
                        } else {
                            //被合并的单元格设置为 0
                            e.mergeColumn = 0
                        }
                    }
                })
          
                monthNums.forEach(n => {
                    if(e._twoDirectory === n._twoDirectory){
                        if(twoDirectorys.includes(e._twoDirectory)){
                            e.mergeColumn1 = n.num
                            //删除已经设置过的值(防止被合并的单元格进到这个 if 语句中)
                            twoDirectorys.splice(twoDirectorys.indexOf(n._twoDirectory),1)
                        } else {
                            //被合并的单元格设置为 0
                            e.mergeColumn1 = 0
                        }
                    }
                })
            })
            return dataTable
          },
          query() {
            this.loading = true
              this.$http.get(this.getUrl+ this.$attrs.id).then((res) => {
                  this.dataTable.splice()
                  this._dataTable = res.data.data.items
                  this.dataTable = this.getTableDate(res.data.data.items)
                  this.loading = false
              })
          },

          saveEvaluate(row,index){
            row.updateDate= new Date(),
            row.updateUserId= this.currentUser.userId,
            row.updateOrgId= this.currentUser.orgInfo.orgId,
            row.updateUserName= this.currentUser.userName,
            this.$http.put(this.itemUpdateUrl, row).then((res) => {
                if (res.data.status === 200) {
                    this.$Message.success('保存成功');
                    this.dataTable[index] = row
                } else if (res.data.status === 201) {
                    this.$Message.error(res.data.msg);
                }
            }).catch(e => {
                this.$Message.error('保存失败')
            })
            
          }
      }
  }
</script>

<style lang='css' scoped>
  .ivu-form-item {
      padding-right: 20px;
  }

  .query-form-el, .ivu-date-picker, .ivu-input-wrapper {
      width: 100%;
  }

</style>